package com.java.day07.weapons;

import java.util.Random;

/**
 * 武器工具类
 * 1. 英雄战胜boss后，获得了5件武器（随机武器，可能有长剑和匕首），写一个方法用来生成5件随机的武器，返回武器数组。
 * 2. 把武器数组按照长剑和匕首进行分类，把长剑放入长剑数组中，把匕首放入匕首数组中。
 */
public class WeaponsUtil {

    /**
     * 随机武器
     * @param length 掉落的个数
     * @return
     */
    public static Weapons[] randomWeapons(int length){
        String[] daggers = {"戒心小刀","火焰匕首","寒冰匕首","雷电匕首","合金匕首"};
        String[] longSwords = {"霜之哀伤","破伤风之刃","骑士双手剑","天顶剑","光剑"};
        //创建武器数组
        Weapons[] weapons1 = new Weapons[length];

        //创建随机数对象
        Random random = new Random();

        for (int i = 0; i < weapons1.length; i++) {
            int j = random.nextInt(2);
            switch (j){
                //掉落匕首,把匕首放到武器数组中
                case 0:

                    //把匕首对象放到武器数组中
                    weapons1[i] = new Dagger();
                    Dagger d = (Dagger) weapons1[i];
                    //名字
                    d.setName(daggers[random.nextInt(daggers.length)]);
                    //攻速
                    d.setAtackSpeed(1+random.nextInt(10));
                    //移速
                    d.setMovSpeed(1+random.nextInt(10));
                    //伤害
                    d.setAtack(100+random.nextInt(101));
                    break;
                case 1:
                    LongSword longSword = new LongSword();
                    //名字
                    longSword.setName(longSwords[random.nextInt(longSwords.length)]);
                    //格挡
                    longSword.setBlock(1+random.nextInt(10));
                    //暴击
                    longSword.setCriticalStrike(1+random.nextInt(10));
                    //伤害
                    longSword.setAtack(100+random.nextInt(101));
                    weapons1[i] = longSword;
                    break;
            }

        }
        return weapons1;
    }

    /**
     * 武器分类
     * @param weapons 武器数组
     * @param daggers 匕首数组
     * @param longSwords 长剑数组
     */
    public static void weaponsType(Weapons[] weapons,Dagger[] daggers,LongSword[] longSwords){

        //下标按顺序存储
        int longSwordIndex = 0;
        int daggerIndex = 0 ;

        for (int i = 0; i < weapons.length; i++) {
            Weapons weapon = weapons[i];
            //如果武器是匕首类
            if (weapon instanceof Dagger){
                daggers[daggerIndex]=(Dagger) weapon;
                daggerIndex+=1;
            }
            //如果武器是长剑类
            if (weapon instanceof LongSword){
                longSwords[longSwordIndex]=(LongSword) weapon;
                longSwordIndex+=1;
            }
        }
    }

}
